import plotly.graph_objects as go
import numpy as np
import sympy as sp

# 定义变量
x = sp.symbols("x")
# 定义函数
f = sp.sin(x) / x

# x正无穷的极限值
limit_f = sp.limit(f, x, sp.oo)
print(f"函数 f(x)=sin(x)/x 在 x为正无穷的极限值为: {limit_f}")

# x=0的极限值
limit_f = sp.limit(f, x, 0)
print(f"函数 f(x)=sin(x)/x 在 x=0 的极限值为: {limit_f}")

# 可视化显示
x_values = np.linspace(0, 2 * np.pi, 200)
y_values = np.sin(x_values) / x_values

fig = go.Figure(data=go.Scatter(x=x_values, y=y_values, mode="lines"))
# 更新布局
fig.update_layout(
    title="f(x) = sin(x)/x 极限值",
    xaxis=dict(
        tickmode="array",
        tickvals=[0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi],
        ticktext=["0", "π/2", "π", "3π/2", "2π"],
    ),
)
fig.show()
